/*
 *
 *  Copyright 2015-2016 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *
 */

package com.xiaouyudeguang.common.plugins;

import com.xiaouyudeguang.common.annotation.ApiLog;
import com.xiaouyudeguang.common.annotation.ApiMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import springfox.documentation.RequestHandler;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
import springfox.documentation.spring.web.DescriptionResolver;
import springfox.documentation.swagger.common.SwaggerPluginSupport;

import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Component
@Order(Ordered.HIGHEST_PRECEDENCE + 2001)
public class ApiNotesReader implements OperationBuilderPlugin {
    private final DescriptionResolver descriptions;

    private final Map<String, Method> requestHandlerMap = new LinkedHashMap<>();

    @Autowired
    public ApiNotesReader(DescriptionResolver descriptions) {
        this.descriptions = descriptions;
    }

    @Override
    public void apply(OperationContext context) {
        if (requestHandlerMap.isEmpty()) {
            List<RequestHandler> requestHandlers = context.getDocumentationContext().getRequestHandlers();
            requestHandlers.forEach(requestHandler -> {
                requestHandlerMap.put(requestHandler.getPatternsCondition().getPatterns().iterator().next().toString(), requestHandler.getHandlerMethod().getMethod());
            });
        }
        Method method = requestHandlerMap.get(context.requestMappingPattern());
        ApiLog apiLog = AnnotationUtils.findAnnotation(method, ApiLog.class);
        if (apiLog != null && StringUtils.hasText(apiLog.tags())) {
            context.operationBuilder()
                    .notes(descriptions.resolve(apiLog.notes()))
                    .summary(descriptions.resolve(apiLog.tags()));
        }
        ApiMapping apiMapping = AnnotationUtils.findAnnotation(method, ApiMapping.class);
        if (apiMapping != null && StringUtils.hasText(apiMapping.tags())) {
            context.operationBuilder()
                    .notes(descriptions.resolve(apiMapping.notes()))
                    .summary(descriptions.resolve(apiMapping.tags()));
        }
    }

    @Override
    public boolean supports(DocumentationType delimiter) {
        return SwaggerPluginSupport.pluginDoesApply(delimiter);
    }
}
